package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
public class GeneralApnSelectAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;


    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        // 取所有接入类产品
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(data.getSceneInstId());
        if (KiteListUtils.isNotEmpty(kiteProdInstList)) {
            List<KiteAttrValue> attrValues = new ArrayList<>();
            kiteProdInstList.stream().forEach(prodInst -> {
                KiteAttr apn = prodInst.getAttr("200004104");
                if (!ObjectUtils.isEmpty(apn)) {
                    KiteAttrValue attrValue = new KiteAttrValue();
                    attrValue.setAttrId(attr.getAttrId());
                    attrValue.setAttrNbr(attr.getAttrNbr());
//                    attrValue.setAttrValue(apn.getValue());
                    // 按外围系统要求，APN资费计划，传名称，不传编码
                    attrValue.setAttrValue(apn.getValueDesc());
                    attrValue.setAttrValueName(apn.getValueDesc());
                    attrValues.add(attrValue);
                }
            });
            attr.setAttrValues(attrValues);
            // 只有一个APN的时候要默认勾选
            if (KiteListUtils.isNotEmpty(attrValues) && attrValues.size() == 1) {
                //准备设置的值
                String to = attrValues.get(0).getAttrValue();
                if (!check(attr, inst, data, new String[] {to})) {
                    attr.setValue(to);
                }
            }
        }
        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);

    }

    /**
     * 校验是否允许设置默认值，已经设置过的就不允许
     * @param attr
     * @param inst
     * @param data
     * @param tos
     * @return true代表存在
     */
    private boolean check(KiteAttr attr, KiteInst inst, AttrData data, String[] tos) {
        if ("sceneTempl".equals(data.getExtAttributes().get("entry"))) {
            return false;
        }
        if ("200004035".equals(attr.getFieldName()) && inst instanceof KiteProdInstFunc) {
            KiteProdInstFunc funcInst = (KiteProdInstFunc) inst;
            List<KiteProdInstFunc> fees = getFeeFunc(data, funcInst);
            //不存在
            if (KiteListUtils.isEmpty(fees)) {
                return false;
            }

            for (KiteProdInstFunc fee : fees) {
                if (fee.getProdInstId().equals(funcInst.getProdInstId())) {
                    continue;
                }
                KiteAttr apnAttr = fee.getAttr(attr.getFieldName());
                if (apnAttr == null || KiteStringUtils.isEmpty(apnAttr.getValue())) {
                    continue;
                }
                String str = apnAttr.getValue();
                List<String> has = Arrays.asList(str.split(","));
                //包含了就
                for (String to :tos){
                    if (has.contains(to)) {
                        return true;
                    }
                }
            }

        }

        return false;
    }

    private List<KiteProdInstFunc> getFeeFunc(AttrData data, KiteProdInstFunc funcInst) {
        //首先获取接入实例，找到下面已经存在的资费
        List<KiteProdInstFunc> kiteProdInstFuncs = querySceneInstService
            .listProdInstFuncs(data.getCustId(), funcInst.getAccProdInstId());
        List<KiteProdInstFunc> fees = KiteListUtils.isEmpty(kiteProdInstFuncs) ?
            null :
            kiteProdInstFuncs.stream().filter(v -> v.getProdId().equals(funcInst.getProdId()))
                .collect(Collectors.toList());
        return fees;
    }
}
